﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Expo.Training.Entity;
using Expo.Training.BRule;
using System.IO;
using Expo.Training.BRule.Access;

public partial class Portal_News_EditNews : PageBase
{
    public int newsId;
    public Guid docid
    {
        get
        {
            if (ViewState["docid"] != null)
            {
                return new Guid(ViewState["docid"].ToString());
            }
            return Guid.Empty;
        }
        set { ViewState["docid"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        newsId = GetIntValue("id");
        if (!IsPostBack)
        {
            InitCecEditor();
            BindNewsInfo(newsId);
            //pn.Visible = false;
            //pdf.Visible = false;
        }
        string customerCode = this.Customer.CustomerCode.ToLower();
        if (customerCode != "sttc")
        {
            //rbt_o.Visible = false;
            rbt_c.Visible = false;
        }

    }

    private void InitCecEditor()
    {
        ED_Editor.Show();
    }


    protected void BindNewsInfo(int id)
    {
        News objModel = new News();
        objModel.NID = id;
        if (objModel.Exist())
        {
            this.txt_Title.Text = objModel.Title;
            ED_Editor.Content = objModel.NewsContent;
            if (objModel.Status == 1)
            {
                this.rbt_m.Checked = true;
            }
            else
            {
                this.rbt_w.Checked = true;
            }

            #region 设置新闻类型

            #region 设置慈溪新闻类型

            if (objModel.Type == 1)
            {
                this.rbt_y.Checked = true;
            }
            else if (objModel.Type == 11)
            {
                this.rbt_n.Checked = true;
                if (objModel.PDFName == "")
                {
                    pdf.Attributes["style"] = "display: table-row";
                }
                else
                {
                    this.txt_pname.Enabled = false;
                    this.txt_pname.Text = objModel.PDFName;
                    pn.Attributes["style"] = "display: table-row";
                }
            }
            else if (objModel.Type == 22)
            {
                this.rbt_p.Checked = true;
            }
            else if (objModel.Type == 55)
                this.rbt_t.Checked = true;
            else
                this.rbt_h.Checked = true;

            #endregion

            #region 设置巴马新闻类型

            if (objModel.Type == 101)
            {
                this.rbt_y_bm.Checked = true;
            }
            else if (objModel.Type == 102)
            {
                this.rbt_n_bm.Checked = true;
                if (objModel.PDFName == "")
                {
                    pdf.Attributes["style"] = "display: table-row";
                }
                else
                {
                    this.txt_pname.Enabled = false;
                    this.txt_pname.Text = objModel.PDFName;
                    pn.Attributes["style"] = "display: table-row";
                }
            }
            else if (objModel.Type == 103)
            {
                this.rbt_p_bm.Checked = true;
            }
            else if (objModel.Type == 104)
                this.rbt_t_bm.Checked = true;
            else
                this.rbt_h.Checked = true;


            #endregion
            #endregion

            if (objModel.IsTop == 1)
            {
                this.rbt_Yes.Checked = true;
                lbl_TopExpired.Attributes["style"] = "";
                txt_TopExpired.Value = objModel.TopExpiredDate.ToString("yyyy-MM-dd");
            }
            else
            {
                this.rbt_No.Checked = true;
                lbl_TopExpired.Attributes["style"] = "display:none;";
                txt_TopExpired.Value = DateTime.Now.AddDays(15).ToString("yyyy-MM-dd");
            }

            this.txt_tag.Text = objModel.Tags;
            if (objModel.ImageUrl.Trim().Length == 0)
            {
                this.lt_Header.Text = "<img src=\"" + ResolveClientUrl("~/Data/PortalNews/Default.gif") + "\" alt=\"\" />";
            }
            else
            {
                this.lt_Header.Text = "<img src=\"" + ResolveClientUrl("~/Data/PortalNews/" + objModel.ImageUrl) + "\" alt=\"\" />";
            }
            docid = objModel.DocID;
            Expo.Training.BRule.Access.AccessConst.AccessLevel levelID = AccessManager.GetMemberAccessLevel(Member.MemberID);
            if (levelID != Expo.Training.BRule.Access.AccessConst.AccessLevel.System)
            {
                string creatorid = objModel.Creator.ToString();
                if (creatorid != Guid.Empty.ToString())
                {
                    Expo.Training.BRule.Access.AccessConst.AccessLevel UserlevelID = AccessManager.GetMemberAccessLevel(new Guid(creatorid));
                    if (UserlevelID == Expo.Training.BRule.Access.AccessConst.AccessLevel.System)
                    {
                        this.btn_Delete.Visible = false;
                        this.btn_UP.Visible = false;
                    }
                    else
                    {
                        string departId = string.Empty;
                        Member_Department objMD = new Member_Department();
                        objMD.MemberID = this.Member.MemberID;
                        if (objMD.Exist())
                        {
                            departId = objMD.DepartmentID.ToString();
                        }
                        objMD.Dispose();
                        Member_Department objMember = new Member_Department();
                        objMember.MemberID = new Guid(creatorid);
                        if (objMember.Exist())
                        {
                            Department_Info objDepartment = new Department_Info();
                            objDepartment.DepartmentID = objMember.DepartmentID;
                            if (objDepartment.Exist())
                            {
                                if (objDepartment.KeyString.ToUpper().IndexOf(departId.ToUpper()) > -1)
                                {
                                    this.btn_UP.Visible = true;
                                }
                                else
                                {
                                    this.btn_Delete.Visible = false;
                                    this.btn_UP.Visible = false;
                                }
                            }
                            objDepartment.Dispose();
                        }
                        objMember.Dispose();
                    }
                }
            }
        }
        objModel.Dispose();
    }

    public void DoDelete(object sender, EventArgs e)
    {
            News objNews = new News();
            objNews.NID = newsId;
            objNews.PDFName = "";
            objNews.Update();
            objNews.Dispose();

            Doc_Info objdc = new Doc_Info();
            objdc.DocID = docid;
            DataTable dt = objdc.QueryDataTable();
            string docURL = dt.Rows[0]["DocURL"].ToString();
            string docPDFURL = dt.Rows[0]["DocPDFURL"].ToString();
            string docSWFURL = dt.Rows[0]["DocSWFURL"].ToString();
            string addr1 = "~/" + docURL;
            addr1 = Server.MapPath(addr1);
            System.IO.File.Delete(addr1);
            if (docPDFURL != "")
            {
                string addr2 = "~/" + docPDFURL;
                addr2 = Server.MapPath(addr2);
                System.IO.File.Delete(addr2);
            }
            if (docSWFURL != "")
            {
                string addr3 = "~/" + docSWFURL;
                addr3 = Server.MapPath(addr3);
                System.IO.File.Delete(addr3);
            }

            Doc_Info dc = new Doc_Info();
            dc.DocID = docid;
            dc.Delete();
            dc.Dispose();

            string url = "EditNews.aspx?id=" + newsId;
            Response.Redirect(url);
    }

    public void DoEditer(object sender, EventArgs e)
    {
        lt_Header.Text = string.Empty;
        string NewsTitle = HttpUtility.HtmlEncode(this.txt_Title.Text.Trim());
        string NewsContent = ED_Editor.Content.Trim();
        DateTime topExpiredDate = DateTime.Parse("1900-1-1");

        if (NewsTitle.Length == 0)
        {
            AddEndScript("input error", "$(\"sp_BulletinT_error\").innerText = \"标题不能为空！\";");
            return;
        }
        if (NewsContent.Length == 0)
        {
            AddEndScript("input error", "$(\"sp_new_error\").innerText = \"内容不能为空！\";");
            return;
        }
        try
        { topExpiredDate = DateTime.Parse(txt_TopExpired.Value); }
        catch { }

        News objNews = new News();
        objNews.NID = newsId;
        objNews.Title = NewsTitle;
        objNews.NewsContent = NewsContent;
        if (this.File1.Value != string.Empty)
        {
            string folderName = ResolveClientUrl("~/Data/PortalNews/");
            if (!Directory.Exists(Server.MapPath(folderName)))
            {
                Directory.CreateDirectory(Server.MapPath(folderName));
            }
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string fileExtension = Path.GetExtension(this.File1.PostedFile.FileName).ToUpper();
            string filePath = Server.MapPath(folderName + fileName + fileExtension);
            if (fileExtension != "" || fileExtension == null)
            {
                if (!(fileExtension == ".JPEG" || fileExtension == ".JPG" || fileExtension == ".PNG" || fileExtension == ".GIF"))
                {
                    this.AddEndScript("error", "alert('不支持的图片格式！');");
                    return;
                }
                this.File1.PostedFile.SaveAs(filePath);
                CutImage image = new CutImage();
                image.CutImageBySize(filePath, Server.MapPath(folderName + fileName + "_n" + fileExtension), 35, 35);
                image.CutImageBySize(filePath, Server.MapPath(folderName + fileName + "_p" + fileExtension), 403, 265);
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                objNews.ImageUrl = fileName + "_n" + fileExtension;
                objNews.PortalImageUrl = fileName + "_p" + fileExtension;
            }
            else
            {
                objNews.ImageUrl = "Default.gif";
            }
        }

        string addr = "";
        string Xls_path = "";
        string savePath = "Data/PortalNews/";
        string name = "";
        string type = this.File2.PostedFile.FileName.Substring(this.File2.PostedFile.FileName.LastIndexOf(".") + 1).ToLower();
        int FileSize = 0;

        Guid docID = Guid.NewGuid();
        objNews.DocID = docID;

        if (this.rbt_y.Checked)
        {
            objNews.Type = 1;
        }
        else if (this.rbt_n.Checked)
        {
            objNews.Type = 11;
            if (type == "pdf")
            {
                name = this.File2.PostedFile.FileName;
                name = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
                string fileName2 = name;//名字
                string path = this.ResolveClientUrl("~/Data/PortalNews/");
                Xls_path = Path.Combine(path, fileName2);//合并路径
                addr = Server.MapPath(Xls_path);//UP路径
                this.File2.PostedFile.SaveAs(addr);//把上传的文档保存到指定的文件夹中
                FileSize = File2.PostedFile.ContentLength;
                savePath = Path.Combine(savePath, fileName2);//获取从Data开始的路径
            }
            else if (type != "")
            {
                AddEndScript("error", "alert('只能上传pdf文件');");
                return;
            }
        }
        else if (this.rbt_p.Checked)
        {
            objNews.Type = 22;
        }
        else if (this.rbt_t.Checked)
        {
            objNews.Type = 55;
        }
        else
        {
            objNews.Type = 44;
        }

        #region 巴马新闻类型

        if (this.rbt_y_bm.Checked)
        {
            objNews.Type = 101;//巴马社区快讯
        }
        else if (this.rbt_n_bm.Checked)
        {
            objNews.Type = 102;//巴马最新公告
            if (type == "pdf")
            {
                name = this.File2.PostedFile.FileName;
                name = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
                string fileName2 = name;//名字
                string path = this.ResolveClientUrl("~/Data/PortalNews/");
                Xls_path = Path.Combine(path, fileName2);//合并路径
                addr = Server.MapPath(Xls_path);//UP路径
                this.File2.PostedFile.SaveAs(addr);//把上传的文档保存到指定的文件夹中
                FileSize = File2.PostedFile.ContentLength;
                savePath = Path.Combine(savePath, fileName2);//获取从Data开始的路径
            }
            else if (type != "")
            {
                AddEndScript("error", "alert('只能上传pdf文件');");
                return;
            }
        }
        else if (this.rbt_p_bm.Checked)
        {
            objNews.Type = 103;//巴马图片新闻
        }
        else if (this.rbt_t.Checked)
        {
            objNews.Type = 104;//巴马时政新闻
        }
        else
        {
            objNews.Type = 44;
        }
        #endregion
        //if (this.rbt_n.Checked)
        //{
        //    objNews.Type = 2;
        //}
        //if (this.rbt_o.Checked)
        //{
        //    objNews.Type = 3;
            //if (type == "pdf")
            //{
            //    name = this.File2.PostedFile.FileName;
            //    name = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
            //    string fileName2 = name;//名字
            //    string path = this.ResolveClientUrl("~/Data/PortalNews/");
            //    Xls_path = Path.Combine(path, fileName2);//合并路径
            //    addr = Server.MapPath(Xls_path);//UP路径
            //    this.File2.PostedFile.SaveAs(addr);//把上传的文档保存到指定的文件夹中
            //    FileSize = File2.PostedFile.ContentLength;
            //    savePath = Path.Combine(savePath, fileName2);//获取从Data开始的路径
            //}
            //else if (type != "")
            //{
            //    AddEndScript("error", "alert('只能上传pdf文件');");
            //    return;
            //}
        //}
        //if (this.rbt_c.Checked)
        //{
        //    objNews.Type = 4;
            //if (type == "pdf")
            //{
            //    name = this.File2.PostedFile.FileName;
            //    name = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
            //    string fileName2 = name;//名字
            //    string path = this.ResolveClientUrl("~/Data/PortalNews/");
            //    Xls_path = Path.Combine(path, fileName2);//合并路径
            //    addr = Server.MapPath(Xls_path);//UP路径
            //    this.File2.PostedFile.SaveAs(addr);//把上传的文档保存到指定的文件夹中
            //    FileSize = File2.PostedFile.ContentLength;
            //    savePath = Path.Combine(savePath, fileName2);//获取从Data开始的路径
            //}
            //else if (type != "")
            //{
            //    AddEndScript("error", "alert('只能上传pdf文件');");
            //    return;
            //}
        //}
        objNews.PDFName = name;
        if (this.rbt_m.Checked)
        {
            objNews.Status = 1;
        }
        if (this.rbt_w.Checked)
        {
            objNews.Status = 0;
        }
        if (this.rbt_Yes.Checked)
        {
            objNews.IsTop = 1;
            objNews.TopExpiredDate = topExpiredDate;
        }
        if (this.rbt_No.Checked)
        {
            objNews.IsTop = 0;
            objNews.TopExpiredDate = DateTime.Parse("1900-1-1");
        }
        objNews.Tags = txt_tag.Text;
        if (objNews.Update())
        {
            if (name != "")
            {
                Doc_Info objDoc = new Doc_Info();
                objDoc.DocID = docID;
                objDoc.DocName = name;
                objDoc.DocURL = savePath;
                objDoc.DocType = type;
                objDoc.DocSize = FileSize;
                objDoc.CreatorID = this.Member.MemberID;
                objDoc.CustomerID = this.Member.CustomerID;
                objDoc.Add();
                objDoc.Dispose();
            }
            Expo.Training.BRule.Audit.Audit.AuditLog(this.Member.MemberID, "新闻管理 - 修改 - ID:" + objNews.NID);
            Response.Redirect("Default.aspx");
        }
        objNews.Dispose();
    }
}
